package com.example.smartwaterapi.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.smartwaterapi.entity.PomodoroClock;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * 番茄闹钟Mapper接口
 */
@Mapper
public interface PomodoroClockMapper extends BaseMapper<PomodoroClock> {
    
    /**
     * 获取用户的番茄钟设置
     */
    @Select("SELECT * FROM pomodoro_clock WHERE user_id = #{userId} AND device_id = #{deviceId}")
    PomodoroClock getUserPomodoroClock(@Param("userId") Long userId, @Param("deviceId") Long deviceId);
    
    /**
     * 更新番茄钟状态
     */
    @Update("UPDATE pomodoro_clock SET current_status = #{status}, " +
            "current_session_start = #{startTime}, current_session_end = #{endTime}, " +
            "remaining_time = #{remainingTime}, updated_at = NOW() " +
            "WHERE user_id = #{userId} AND device_id = #{deviceId}")
    int updatePomodoroStatus(@Param("userId") Long userId, 
                           @Param("deviceId") Long deviceId, 
                           @Param("status") Integer status, 
                           @Param("startTime") LocalDateTime startTime, 
                           @Param("endTime") LocalDateTime endTime, 
                           @Param("remainingTime") Integer remainingTime);
    
    /**
     * 完成一个番茄周期
     */
    @Update("UPDATE pomodoro_clock SET " +
            "today_completed_pomodoros = today_completed_pomodoros + 1, " +
            "today_total_work_time = today_total_work_time + #{workTime}, " +
            "total_completed_pomodoros = total_completed_pomodoros + 1, " +
            "total_work_time = total_work_time + #{workTime}, " +
            "updated_at = NOW() " +
            "WHERE user_id = #{userId} AND device_id = #{deviceId}")
    int completePomodoro(@Param("userId") Long userId, @Param("deviceId") Long deviceId, @Param("workTime") Integer workTime);
    
    /**
     * 重置今日统计数据
     */
    @Update("UPDATE pomodoro_clock SET " +
            "today_completed_pomodoros = 0, " +
            "today_total_work_time = 0, " +
            "today_date = #{today}, " +
            "updated_at = NOW() " +
            "WHERE user_id = #{userId} AND device_id = #{deviceId} AND (today_date IS NULL OR today_date < #{today})")
    int resetDailyStats(@Param("userId") Long userId, @Param("deviceId") Long deviceId, @Param("today") LocalDate today);
    
    /**
     * 更新连续使用天数
     */
    @Update("UPDATE pomodoro_clock SET " +
            "current_streak = current_streak + 1, " +
            "longest_streak = GREATEST(longest_streak, current_streak + 1), " +
            "updated_at = NOW() " +
            "WHERE user_id = #{userId} AND device_id = #{deviceId} AND today_date = DATE_SUB(#{today}, INTERVAL 1 DAY)")
    int updateStreak(@Param("userId") Long userId, @Param("deviceId") Long deviceId, @Param("today") LocalDate today);
    
    /**
     * 重置连续使用天数
     */
    @Update("UPDATE pomodoro_clock SET " +
            "current_streak = 1, " +
            "updated_at = NOW() " +
            "WHERE user_id = #{userId} AND device_id = #{deviceId} AND today_date < DATE_SUB(#{today}, INTERVAL 1 DAY)")
    int resetStreak(@Param("userId") Long userId, @Param("deviceId") Long deviceId, @Param("today") LocalDate today);
} 